package com.home.controller;
import com.home.ex.RabbitmqConfigDirect;
import com.home.ex.RabbitmqConfigFanout;
import com.home.ex.RabbitmqConfigTopics;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SenderController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @PostMapping("/sender")
    public String senderHello(@RequestParam String message) {
        /**
         * 参数说明
         * exchnage: 交换机，默认交换机指定为“”即可
         * routingKey ：发送消息的路由键，该模式下使用队列名即可
         * message:消息的内容
         */
        rabbitTemplate.convertAndSend( RabbitmqConfigDirect.EXCHANGE_DIRECT,"pay",message);
        rabbitTemplate.convertAndSend( RabbitmqConfigDirect.EXCHANGE_DIRECT,"order",message);
        return "success";
    }

    @PostMapping("/topic")
    public String senderTopic(@RequestParam String message) {
        // 发送消息到不同的路由键
        rabbitTemplate.convertAndSend(RabbitmqConfigTopics.EXCHANGE_TOPIC, "pay.success", message);
        rabbitTemplate.convertAndSend(RabbitmqConfigTopics.EXCHANGE_TOPIC, "pay.fall", message);
        rabbitTemplate.convertAndSend(RabbitmqConfigTopics.EXCHANGE_TOPIC, "order.success", message); // 修改为 hello.order
        rabbitTemplate.convertAndSend(RabbitmqConfigTopics.EXCHANGE_TOPIC, "order.fall", message); // 修改为 baby.order
        return "success";
    }
}

